package com.project.advertisement.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.project.R;
import com.project.advertisement.dao.AdvertisementDao;
import com.project.advertisement.property.Properties;
import com.project.advertisement.util.Utility;


public class RegisterEmailActivity extends Activity {
	private EditText txtEmailId;
	private AdvertisementDao advertisementDao;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_email);
		txtEmailId = (EditText) findViewById(R.id.txtEmail);
		advertisementDao=new AdvertisementDao(getApplicationContext(),Properties.databaseName, null, 1);
	}

	public void saveEmail(View view) {
		String email=txtEmailId.getText().toString().trim();
		if (!Utility.validateEmail(email)){
			Utility.showErrorMessage(Properties.emailFormatErrorMessage,
					RegisterEmailActivity.this);
			return;
		}
		if (advertisementDao.isEmailRegistered(email)){
			Utility.showErrorMessage(Properties.emailRegistrationErrorMessage,
					RegisterEmailActivity.this);
			txtEmailId.setText("");
			return;
		}
		long rowId=advertisementDao.registerEmail(email);
		if(rowId>0){
			Toast.makeText(getApplicationContext(), Properties.emailSuccessfullyRegisteredMessage, Toast.LENGTH_LONG).show();
			txtEmailId.setText("");
		}
		

	}

}
